package CtrLayer;
import ModLayer.*;
import java.util.ArrayList;

/**
 * Write a description of class PersonCtr here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PersonCtr
{
    public static PersonCtr instance;
    private PersonCon personCon;
    private TimeAndCheck time;

    private PersonCtr()
    {
        personCon = personCon.getInstance();
        time = time.getInstance();
        createCompany();
    }
    
    public static PersonCtr getInstance()
    {
        if(instance==null)
            instance = new PersonCtr();
        return instance;
    }

    public void createCustomer(String name, String email, String address, String phoneNo)
    {
        String customerNo = time.getNoForCustomer();
        Customer customer = new Customer(name, email, address, phoneNo, customerNo);
        personCon.add(customer);
    }

    public void createEmployee(String name, String email, String address, String phoneNo,
    String job, String bankAccountNo, String id, String salary)
    {
        Employee employee = new Employee(name, email, address, phoneNo, job,
        bankAccountNo, id, salary);

        personCon.add(employee);
    }

    private void createCompany()
    {
        if(searchPerson("Company", "office")==null)
            personCon.add(Company.getInstance());
    }

    public ArrayList<PersonInformations> getPeopleFromSpecificClass(String nameClass)
    {
        ArrayList<PersonInformations> person = new ArrayList<PersonInformations>();
        for(PersonInformations personObj:personCon.getPersonList())
        {
            if(personObj.toString().equals(nameClass))
                person.add(personObj);
        }
        return person;
    }

    public void deletePerson(String data, String privileges)
    {
        int index = 0;
        while(index<personCon.getPersonList().size())
        {
            PersonInformations personConObj = personCon.getPersonList().get(index);
            if(personConObj instanceof Customer)
            {
                if(privileges.equals("office") || privileges.equals("employee"))
                {
                    Customer cObj = (Customer) personConObj;
                    if(cObj.getCustomerNumber().equals(data))
                        personCon.getPersonList().remove(index);
                }
            }
            else if(personConObj instanceof Employee)
            {
                if(privileges.equals("office"))
                {
                    Employee cObj = (Employee) personConObj;
                    if(cObj.getID().equals(data))
                        personCon.getPersonList().remove(index);
                }
            }
            index++;
        }
    }

    public PersonInformations searchPerson(String data, String privileges)
    {
        int index = 0;
        boolean found = false;
        PersonInformations person = null;
        while(index<personCon.getPersonList().size() && !found)
        {
            PersonInformations personConObj = personCon.getPersonList().get(index);
            if(personConObj instanceof Customer)
            {
                if(privileges.equals("office") || privileges.equals("employee"))
                {
                    Customer cObj = (Customer) personConObj;
                    if(cObj.getCustomerNumber().equals(data))
                    {
                        person = personConObj;
                        found = true;
                    }
                }
            }
            else if(personConObj instanceof Employee)
            {
                if(privileges.equals("office"))
                {
                    Employee cObj = (Employee) personConObj;
                    if(cObj.getID().equals(data))
                    {
                        person = personConObj;
                        found = true;
                    }
                }
            }
            else if(personConObj.toString().equals(data))
            {
                person = personConObj;
                found = true;
            }
            index++;
        }
        return person;
    }

    public void updateCustomer(String customerNo, String newData, String whichInfo)
    {
        PersonInformations personConObj = searchPerson(customerNo, "office");
        Customer cuObj = (Customer) personConObj;
        if(personConObj != null)
        {
            switch(whichInfo)
            {
                case "0": personConObj.setAddress(newData); break;
                case "1": personConObj.setEmail(newData); break;
                case "2": personConObj.setName(newData); break;
                case "3": personConObj.setPhoneNo(newData); break;
                case "4": cuObj.setOwe(newData); break;
            }
        }
    }

    public void updateEmployee(String id, String newData, String whichInfo)
    {
        PersonInformations personConObj = searchPerson(id, "office");
        Employee eObj = (Employee) personConObj;
        if(personConObj != null)
        {
            switch(whichInfo)
            {
                case "0": personConObj.setAddress(newData); break;
                case "1": personConObj.setEmail(newData); break;
                case "2": personConObj.setName(newData); break;
                case "3": personConObj.setPhoneNo(newData); break;
                case "4": eObj.setBankAccountNo(newData); break;
                case "5": eObj.setID(newData); break;
                case "6": eObj.setSalary(newData); break;
                case "7": eObj.setJob(newData); break;
                case "8": eObj.setTurnover(newData); break;
            }
        }
    }

    public boolean updateCompany(String newData, String whichInfo)
    {
        PersonInformations personConObj = searchPerson("Company", "office");
        Company cObj = (Company) personConObj;
        boolean ok = false;
        if(personConObj != null)
        {
            switch(whichInfo)
            {
                case "0": personConObj.setAddress(newData); break;
                case "1": personConObj.setEmail(newData); break;
                case "2": personConObj.setName(newData); break;
                case "3": personConObj.setPhoneNo(newData); break;
                case "4": cObj.setCompanyNo(newData); break;
                case "5": cObj.setFaxNo(newData); break;
                case "6": ok = cObj.setFixedDiscount(newData); break;
            }
        }
        return ok;
    }

    public String getInfoCompany(String whichInfo)
    {
        PersonInformations personConObj = searchPerson("Company", "office");
        Company cObj = (Company) personConObj;
        String info = null;
        if(personConObj != null)
        {
            switch(whichInfo)
            {
                case "0": info = personConObj.getAddress(); break;
                case "1": info = personConObj.getEmail(); break;
                case "2": info = personConObj.getName(); break;
                case "3": info = personConObj.getPhoneNo(); break;
                case "4": info = cObj.getCompanyNo(); break;
                case "5": info = cObj.getFaxNo(); break;
                case "6": info = cObj.getfixedDiscount(); break;
            }
        }
        return info;
    }

    public String getInfoEmployee(String id, String whichInfo)
    {
        PersonInformations personConObj = searchPerson(id, "office");
        Employee eObj = (Employee) personConObj;
        String info = null;
        if(personConObj != null)
        {
            switch(whichInfo)
            {
                case "0": info = personConObj.getAddress(); break;
                case "1": info = personConObj.getEmail(); break;
                case "2": info = personConObj.getName(); break;
                case "3": info = personConObj.getPhoneNo(); break;
                case "4": info = eObj.getBankAccountNo(); break;
                case "5": info = eObj.getID(); break;
                case "6": info = eObj.getSalary(); break;
                case "7": info = eObj.getJob(); break;
                case "8": info = eObj.getTurnover(); break;
            }
        }
        return info;
    }

    public String getInfoCustomer(String customerNo, String whichInfo)
    {
        PersonInformations personConObj = searchPerson(customerNo, "office");
        Customer cuObj = (Customer) personConObj;
        String info = null;
        if(personConObj != null)
        {
            switch(whichInfo)
            {
                case "0": info = personConObj.getAddress(); break;
                case "1": info = personConObj.getEmail(); break;
                case "2": info = personConObj.getName(); break;
                case "3": info = personConObj.getPhoneNo(); break;
                case "4": info = cuObj.getCustomerNumber(); break;
                case "5": info = cuObj.getDateOfRegistration(); break;
            }
        }
        return info;
    }

    public String savePeopleData(String _0_isSave_1_isLoad)
    {
        String error = null;
        switch(_0_isSave_1_isLoad)
        {
            case "0":
            try
            {
                personCon.saveToFile();
            }
            catch(Exception e)
            {
                error = ("unable to save to file - "+e);
            }
            break;

            case "1":
            try
            {
                personCon.loadFromFile();
            }
            catch(Exception e)
            {
                error = ("unable to load from file - "+e);
            }
            break;
        }
        return error;
    }
}